import api from '../../api/api'
import Toast from '@vant/weapp/toast/toast'
import Dialog from '@vant/weapp/dialog/dialog'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isActive: 0,
    tabs: [
      {
        title: '全部',
        status: 0
      },
      {
        title: '进行中',
        status: 1
      },
      {
        title: '已完成',
        status: 2
      },
    ],
    arr: []
  },
  getItem(e) {
    let item = e.currentTarget.dataset.item
    let index = e.currentTarget.dataset.index
    this.setData({
      isActive: index
    })
    this.findOrder(index)
  },
  async findOrder(status) {
    let tokenString = wx.getStorageSync('token')
    const res = await api.findOrder({ tokenString, status })
    console.log('查询订单', res)
    let processedData = res.data.result.reduce((acc, item) => {
      let { oid, time, totalPrice, totalNum, status, ...productData } = item
      let index = acc.findIndex(obj => obj.oid === oid)
      if (index !== -1) {
        acc[index].item.push(productData)
      } else {
        acc.push({
          oid: oid,
          time: time,
          totalPrice: totalPrice,
          totalNum: totalNum,
          status, status,
          item: [productData]
        })
      }
      return acc
    }, [])
    let time = ''
    processedData.forEach((v) => {
      let totalPrice = 0
      let totalNum = 0
      v.item.forEach((item) => {
        // 处理时间
        let demo = new Date(item.createdAt)
        let year = demo.getFullYear()
        let month = demo.getMonth() + 1
        let day = demo.getDate()
        let hour = demo.getHours()
        let minute = demo.getMinutes()
        let second = demo.getSeconds()
        time = year + '-' + (month < 10 ? '0' + month : month) + '-' + (day < 10 ? '0' + day : day) + ' ' + (hour < 10 ? '0' + hour : hour) + ':' + (minute < 10 ? '0' + minute : minute) + ':' + (second < 10 ? '0' + second : second)
        // 处理总价格
        totalPrice += item.price * item.count
        // 处理总数量
        totalNum += item.count

      })
      v.time = time
      v.totalPrice = totalPrice.toFixed(2)
      v.totalNum = totalNum
    })
    this.setData({
      arr: processedData
    })
  },
  async receive(e) {
    let tokenString = wx.getStorageSync('token')
    let oid = e.currentTarget.dataset.item.oid
    const res = await api.receive({ oid, tokenString })
    Dialog.confirm({
      message: '确定收到货了？',
    }).then(() => {
      if (res.data.code == 80000) {
        console.log('确认收货', res)
        Toast.success(res.data.msg)
        this.findOrder(this.data.isActive)
      }
    }).catch(() => {
    })
  },
  async del(e) {
    let tokenString = wx.getStorageSync('token')
    let oid = e.currentTarget.dataset.item.oid
    const res = await api.removeOrder({ oid, tokenString })
    Dialog.confirm({
      message: '确定要删除该订单吗？',
    }).then(() => {
      if (res.data.code == 90000) {
        console.log('删除订单', res)
        Toast.success(res.data.msg)
        this.findOrder(this.data.isActive)
      }
    }).catch(() => {
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.findOrder(this.data.isActive)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.findOrder(this.data.isActive)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})